package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.email.AuditExceptionEmailDTO;
import com.etone.smartAudit.dto.email.EmailDTO;
import com.etone.smartAudit.service.EmailRecordService;
import com.etone.smartAudit.service.EmailService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.email.EmailRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

/**
 * @author: 蒋学伟
 * @create: 2020-07-06 10:14:47
 * @Description 邮件管理
 */
@RestController
@AllArgsConstructor
@Api(tags = "邮件管理", value = "EmailController")
public class EmailController {

    private EmailService emailService;

    private EmailRecordService emailRecordService;

    @ApiOperation(value = "发送邮件")
    @PostMapping("/emails")
    public void send(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "邮件内容") @RequestBody @Validated EmailDTO emailDTO
    ) {
        emailService.sent(emailDTO, admin);
    }

    @AdminOperation(module = "持续审计", item = "发送邮件")
    @ApiOperation(value = "发送持续审计邮件")
    @PostMapping("/emails/auditException")
    public void auditExceptionSend(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "邮件内容") @RequestBody @Validated AuditExceptionEmailDTO emailDTO
    ) {
        emailService.auditExceptionSend(emailDTO, admin);
    }

    @ApiOperation(value = "邮件记录")
    @GetMapping("/emails/records")
    public PagerVO<EmailRecordVO> list(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "标题") @RequestParam(required = false) String subject,
            @ApiParam(value = "发送时间-开始时间") @RequestParam(required = false) Date createTimeStartTime,
            @ApiParam(value = "发送时间-结束时间") @RequestParam(required = false) Date createTimeEndTime,
            @ApiParam(value = "发送状态") @RequestParam(required = false) Boolean isSuccess,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return emailRecordService.list(admin, subject, createTimeStartTime, createTimeEndTime, isSuccess, page, pageSize);
    }
}
